﻿Public Class DformaContacto

    Public Shared Function SeleccionarTodo(ByVal descEmpresa As String) As List(Of EformaContacto)
        Dim contactos As List(Of EformaContacto) = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_formaContacto_sel", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            contactos = New List(Of EformaContacto)
                            Dim contacto As EformaContacto = Nothing
                            Do While dr.Read
                                contacto = New EformaContacto( _
                                    dr.GetInt32(dr.GetOrdinal("idFormaContacto")), _
                                    dr.GetString(dr.GetOrdinal("descFormaContacto")), _
                                    dr.GetString(dr.GetOrdinal("estado")))
                                contactos.Add(contacto)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return contactos
    End Function
    Public Shared Function SeleccionarXEstado(ByVal descEmpresa As String) As List(Of EformaContacto)
        Dim contactos As List(Of EformaContacto) = Nothing

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_formaContacto_selXEstado", cn)
                cmd.CommandType = CommandType.StoredProcedure
                Try
                    cn.Open()
                    Using dr As SqlDataReader = cmd.ExecuteReader(CommandBehavior.SingleResult Or CommandBehavior.CloseConnection)
                        If dr.HasRows Then
                            contactos = New List(Of EformaContacto)
                            Dim contacto As EformaContacto = Nothing
                            Do While dr.Read
                                contacto = New EformaContacto( _
                                    dr.GetInt32(dr.GetOrdinal("idFormaContacto")), _
                                    dr.GetString(dr.GetOrdinal("descFormaContacto")), _
                                    dr.GetString(dr.GetOrdinal("estado")))
                                contactos.Add(contacto)
                            Loop
                            dr.Close()
                        End If
                    End Using
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return contactos
    End Function
    Public Shared Function Añadir(ByVal descEmpresa As String, ByVal pcontacto As EformaContacto) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_formaContacto_add", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@descFormaContacto", SqlDbType.VarChar, 55).Value = pcontacto.descFormaContacto
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pcontacto.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Editar(ByVal descEmpresa As String, ByVal pcontacto As EformaContacto) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_formaContacto_edit", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idFormaContacto", SqlDbType.Int).Value = pcontacto.idFormaContacto
                cmd.Parameters.Add("@descFormaContacto", SqlDbType.VarChar, 55).Value = pcontacto.descFormaContacto
                cmd.Parameters.Add("@estado", SqlDbType.Char, 1).Value = pcontacto.estado
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pcontacto.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function
    Public Shared Function Eliminar(ByVal descEmpresa As String, ByVal pcontacto As EformaContacto) As Boolean
        Dim resultado As Boolean = False

        Using cn As New SqlConnection(ConfigurationManager.ConnectionStrings(descEmpresa).ConnectionString)
            Using cmd As New SqlCommand("usp_formaContacto_del", cn)
                cmd.CommandType = CommandType.StoredProcedure
                cmd.Parameters.Add("@idFormaContacto", SqlDbType.Int).Value = pcontacto.idFormaContacto
                cmd.Parameters.Add("@idUsuario", SqlDbType.Int).Value = pcontacto.idUsuario
                Try
                    cn.Open()
                    cmd.ExecuteNonQuery()
                    resultado = True
                Catch ex As Exception
                    Throw
                End Try
            End Using
        End Using

        Return resultado
    End Function

End Class
